#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#include<unordered_set>
#include<unordered_map>
using namespace std;


struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode() : val(0), left(nullptr), right(nullptr) {}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
	TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
	TreeNode* invertTree(TreeNode* root) {
		if(root == NULL) return root;
		stack<TreeNode*> st;
		st.push(root);
		while (!st.empty()) 
		{
			TreeNode* node = st.top();
			st.pop();
			swap(node->left, node->right);
			if (node->right) st.push(node->right);
			if (node->left) st.push(node->left);
		}
		return root;
	}
};